class ChatWebSocketClient {
    constructor(options = {}) {
        this.options = {
            serverUrl: options.serverUrl || 'ws://localhost:8080/ws',
            reconnectAttempts: options.reconnectAttempts || 5,
            reconnectInterval: options.reconnectInterval || 3000,
            heartbeatInterval: options.heartbeatInterval || 30000
        };
        
        this.ws = null;
        this.connected = false;
        this.reconnectCount = 0;
        this.heartbeatTimer = null;
        this.messageHandlers = new Map();
        this.userId = localStorage.getItem('userId') || this.generateUserId();
        
        this.init();
    }
    
    // 初始化WebSocket连接
    init() {
        try {
            this.ws = new WebSocket(`${this.options.serverUrl}?userId=${this.userId}`);
            
            this.ws.onopen = (event) => {
                console.log('WebSocket连接成功');
                this.connected = true;
                this.reconnectCount = 0;
                this.startHeartbeat();
                this.notifyHandlers('connected', event);
            };
            
            this.ws.onmessage = (event) => {
                this.handleMessage(event);
            };
            
            this.ws.onclose = (event) => {
                console.log('WebSocket连接关闭');
                this.connected = false;
                this.stopHeartbeat();
                this.notifyHandlers('disconnected', event);
                this.handleReconnect();
            };
            
            this.ws.onerror = (error) => {
                console.error('WebSocket错误:', error);
                this.notifyHandlers('error', error);
            };
            
        } catch (error) {
            console.error('WebSocket初始化失败:', error);
            this.handleReconnect();
        }
    }
    
    // 处理接收到的消息
    handleMessage(event) {
        try {
            const messageData = JSON.parse(event.data);
            const { type, conversationId, senderId, content, messageType, timestamp } = messageData;
            
            switch (type) {
                case 'new_message':
                    this.notifyHandlers('new_message', messageData);
                    this.displayNewMessage(messageData);
                    break;
                    
                case 'message_recall':
                    this.handleMessageRecall(messageData);
                    break;
                    
                case 'message_read':
                    this.handleMessageRead(messageData);
                    break;
                    
                case 'typing':
                    this.handleTypingStatus(messageData);
                    break;
                    
                case 'user_online':
                    this.handleUserStatusChange(messageData, true);
                    break;
                    
                case 'user_offline':
                    this.handleUserStatusChange(messageData, false);
                    break;
                    
                default:
                    console.log('未知消息类型:', type);
            }
        } catch (error) {
            console.error('消息处理错误:', error);
        }
    }
}